<?php

$itemId = $_GET['itemId'];

if (((strlen($itemId))%2)==0) {
	$new_string		= "";
	$lbc			= 0;
	$xi				= 0; 
	$k				= 0;
	$bc_string		= $itemId;
	$widthSlimBar	= 1;
	$widthFatBar	= 2;
	$BarCodeHeight	= 20;
	$bc				= array();

	$bc[0]  = "00110";         //0 digit
	$bc[1]  = "10001";         //1 digit
	$bc[2]  = "01001";         //2 digit
	$bc[3]  = "11000";         //3 digit
	$bc[4]  = "00101";         //4 digit
	$bc[5]  = "10100";         //5 digit
	$bc[6]  = "01100";         //6 digit
	$bc[7]  = "00011";         //7 digit
	$bc[8]  = "10010";         //8 digit
	$bc[9]  = "01010";         //9 digit
	$bc[10] = "0000";          //pre-amble
	$bc[11] = "100";           //post-amble

	$bc_string = strtoupper($bc_string);

	$lbc = strlen($bc_string) - 1;
	for($xi=0; $xi<=$lbc; $xi++) {
		$k = (int) substr($bc_string,$xi,1);
		$new_string = $new_string . $bc[$k];
	}
	$bc_string = $new_string;
	
	$i = 0; 
	$l = 0; 
	$s = "";                 
	
	$l=strlen($bc_string);		
	for ($i=0;$i<$l;$i+=10) {
		$s = $s . $bc_string[$i]   .  $bc_string[$i+5];
		$s = $s . $bc_string[$i+1] .  $bc_string[$i+6];
		$s = $s . $bc_string[$i+2] .  $bc_string[$i+7];
		$s = $s . $bc_string[$i+3] .  $bc_string[$i+8];
		$s = $s . $bc_string[$i+4] .  $bc_string[$i+9];
	}
	$bc_string = $s;
	$bc_string = $bc[10] .$bc_string.$bc[11];
			
	$lbc = strlen($bc_string) - 1;
	$imWidth = ($l*1.9);

	$im = ImageCreate($imWidth, 60);
	$black = ImageColorAllocate($im, 0, 0, 0);
	$white = ImageColorAllocate($im, 255, 255, 255);
	ImageFill($im, 0, 0, $white);
	$Xposition = 2;

	for( $xi=0; $xi<= $lbc; $xi++ ) {
		$imgBar = "";
		$imgWid = 0;
		$imgBar = ( $xi % 2 == 0 ) ? $black : $white;
		$imgWid = ( $bc_string[$xi]=="0" ) ? $widthSlimBar : $widthFatBar;
		$end_y = $BarCodeHeight;
		$end_y = $end_y + 5;
		for($qw=0;$qw< $imgWid;$qw++) {
			ImageLine($im, ($Xposition), 1, ($Xposition), $end_y, $imgBar);
			$Xposition++;
		}
	}
	$itemId = trim($itemId);
	ImageString($im,1, 10, 30, "$itemId", $black);		
	ImagePNG($im);
}


?>